//
//  ShapedImageView.m
//  ShapedImageView
//
//  Created by Sword on 11/16/14.
//  Copyright (c) 2014 Sword. All rights reserved.
//

#import "HJShapedImageView.h"

@interface HJShapedImageView()
{
    CALayer      *_contentLayer;
    CAShapeLayer *_maskLayer;
}
@end

@implementation HJShapedImageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}
-(void)setTagLabel:(UILabel *)tagLabel
{
    [self addSubview:tagLabel];
}
//-(UILabel *)tagLabel
//{
//    if (!_tagLabel) {
//        _tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, Nomal_Label_H)];
//        _tagLabel.font = [UIFont systemFontOfSize:12];
//        _tagLabel.textColor = [UIColor whiteColor];
//        [_tagLabel sizeToFit];
//        CGFloat width = _tagLabel.frame.size.width;
//        _tagLabel.frame = CGRectMake(0, 0, width, Nomal_Label_H);
//        
//    }
//    [self addSubview:_tagLabel];
//    return _tagLabel;
//}
//-(void)setTagLabel:(UILabel *)tagLabel
//{
//
//}

- (void)setup
{
    [_contentLayer removeFromSuperlayer];
    _maskLayer = [CAShapeLayer layer];
    _maskLayer.fillColor = [UIColor blackColor].CGColor;
    _maskLayer.strokeColor = [UIColor clearColor].CGColor;
    _maskLayer.frame = self.bounds;
    _maskLayer.contentsCenter = CGRectMake(0.4, 0, 0.1, 1);//x,y,widt,height
    _maskLayer.contentsScale = [UIScreen mainScreen].scale;                 //非常关键设置自动拉伸的效果且不变形
    _maskLayer.contents = (id)[UIImage imageNamed:@"s_Rectangle"].CGImage;
    _contentLayer = [CALayer layer];
    _contentLayer.mask = _maskLayer;
    _contentLayer.frame = self.bounds;
    [self.layer addSublayer:_contentLayer];
    
}
- (void)setupTag:(BOOL)isLeft
{
    [_contentLayer removeFromSuperlayer];
    _maskLayer = [CAShapeLayer layer];
    _maskLayer.fillColor = [UIColor blackColor].CGColor;
    _maskLayer.strokeColor = [UIColor clearColor].CGColor;
    _maskLayer.frame = self.bounds;
    _maskLayer.contentsCenter = CGRectMake(0.5, 0, 0.1, 1);
    _maskLayer.contentsScale = [UIScreen mainScreen].scale;                 //非常关键设置自动拉伸的效果且不变形
    if (isLeft) {
        _maskLayer.contents = (id)[UIImage imageNamed:@"left_pay"].CGImage;
    }else{
        _maskLayer.contents = (id)[UIImage imageNamed:@"right_pay"].CGImage;
    }
    
    _contentLayer = [CALayer layer];
    _contentLayer.mask = _maskLayer;
    _contentLayer.frame = self.bounds;
    [self.layer addSublayer:_contentLayer];
    
}
- (void)setImage:(UIImage *)image
{
    
    [self setup];
    _contentLayer.contents = (id)image.CGImage;
}

- (void)setL_tagImage:(UIImage *)l_tagImage
{
    
    [self setupTag:YES];
    _contentLayer.contents = (id)l_tagImage.CGImage;
}
- (void)setR_tagImage:(UIImage *)r_tagImage
{
    
    [self setupTag:NO];
    _contentLayer.contents = (id)r_tagImage.CGImage;
}
@end
